package com.bigdata;

import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 二倍均值法
 *
 * random( 2 * 均值)
 */

public class RedPacket {
    //红包金额
    private int amount;

    //红包剩余个数
    private volatile int count;

    //红包总个数
    private int total;

    private Random random = new Random();

    //创建红包
    public RedPacket(int amount, int total) {
        this.amount  = amount;
        this.count = total;
        this.total = total;
    }


    public synchronized int receivePacket() {
        if (this.count == 1) {
            this.count --;
            System.out.println("本次枪的红包：" + this.amount);
            return this.amount;
        }
        else {
            int highest = ((this.amount) /this.count ) * 2;
            int receiveAmount  = random.nextInt(highest);
            this.amount -= receiveAmount;
            count--;
            System.out.println("本次枪的红包：" + receiveAmount);
            return receiveAmount;
        }

    }





}
